<?php
		require_once('PHPMailer-master/class.phpmailer.php');
		include "conexao.php" ;

		$nrped = $_COOKIE["pedido"];

		$stgsql = $conec->prepare("select a.nome, a.valor ,b.codigo, b.numero
							   from PIZZAS a, PEDIDOS b
							   where  a.codigo= b.codigo and b.numero=:nrped");  
	
		$stgsql->execute(array(':nrped' => "$nrped"));
		$valortotal = 0 ;
		
		$descmail = "Nome do cliente: ".$_POST['txnome']. "<br>";
		$descmail .= "Endereço de Entrega: ".$_POST['txdesc']."<br>";
				
		while ($dados = $stgsql->fetch()) 
		{  
			$descmail .="código: ".$dados['codigo']." nome: ".$dados['nome']."<br>";
			$valortotal += $dados['valor'];
		}
		$descmail .= "Valor total: ".$valortotal. "<br>";
		
		$mail             = new PHPMailer();

		$body             = "<html><body>".$descmail."</html></body>";
	
		$mail->IsSMTP(); // telling the class to use SMTP
		$mail->Host       = "smtp.gmail.com"; // SMTP server
		$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
		  // 1 = errors and messages
		  // 2 = messages only
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
		$mail->Username   = "pizzasonlineudesc@gmail.com";  // GMAIL username
		$mail->Password   = "udesc123";            // GMAIL password

		$mail->SetFrom('mail@gmail.com', 'Pizza Online');
		
		$mail->AddReplyTo("mail2@gmail.com");

		$mail->Subject    = "Pedido: ".$nrped;

		$mail->AltBody    = ""; // optional, comment out and test

		$mail->MsgHTML($body);
				
		$sqlmail = $conec->prepare(" SELECT email FROM usu");  
		$sqlmail->execute();
				
		while ($dadosmail = $sqlmail->fetch()){
			$mail->AddAddress($dadosmail['email'], "teste"); 			
		}
		
		if(!$mail->Send()) {
		  error_log("Mailer Error: " . $mail->ErrorInfo);
		} else {
		  error_log("Message sent!");
		}
		
		setcookie('pedido','');
		echo "<META HTTP-EQUIV='REFRESH' CONTENT=\"0; URL='sucessopedido.php'\">" ;
 
?>
	